package com.lykj.llw.box.ui.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import app.dinus.com.itemdecoration.LinearDividerItemDecoration
import com.blankj.utilcode.util.GsonUtils
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.lykj.lib_base.base.BaseActivity
import com.lykj.lib_base.internal.extfun.clickWithTrigger
import com.lykj.lib_base.internal.utils.ToastUtils
import com.lykj.lib_base.net.http.CustomResponseBodyObserver
import com.lykj.lib_base.net.http.RetrofitManager
import com.lykj.llw.box.R
import com.lykj.llw.box.adapter.GameActivityAdapter
import com.lykj.llw.box.adapter.PlayingAdapter
import com.lykj.llw.box.data.bean.response.GameActivityResponse
import com.lykj.llw.box.data.bean.response.GameDetailResponse
import com.lykj.llw.box.data.bean.response.PlayingListResponse
import com.lykj.llw.box.databinding.ActGameActivityBinding
import io.reactivex.disposables.Disposable
import okhttp3.ResponseBody

/**
 * ================================================
 * 游戏活动/公告
 * ================================================
 *
 * @author gosha
 * @date 2023/10/30-10:15
 */
class GameActActivity : BaseActivity() {
    lateinit var binding: ActGameActivityBinding

    private val mAdapter = GameActivityAdapter()
    private val mData = ArrayList<GameActivityResponse.DataDTO>()
    private var mId = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActGameActivityBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initUI()
        initData()
        setListener()
    }

    override fun onResume() {
        super.onResume()

        ImmersionBar.with(this)
            .transparentBar()
            .statusBarDarkFont(true) //状态栏字体是深色，不写默认为亮色
            .navigationBarDarkIcon(true)
            .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
            .navigationBarEnable(false)
            .init()
    }

    override fun initUI() {
        mId = intent.getIntExtra("gid", 0)

        binding.titleBar.tvTitle.text = "攻略与公告"
        binding.titleBar.tvRightOption.visibility = View.GONE

        initRCV()
    }

    private fun initRCV() {
        val linearLayoutManager = LinearLayoutManager(this)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        binding.rcvActivity.layoutManager = linearLayoutManager
        binding.rcvActivity.setHasFixedSize(true)

        val dividerItemDecoration = LinearDividerItemDecoration(
            this, LinearDividerItemDecoration.LINEAR_DIVIDER_VERTICAL
        )
        dividerItemDecoration.setDivider(
            resources.getDrawable(
                R.drawable.divider_10dp, resources.newTheme()
            )
        )
        binding.rcvActivity.addItemDecoration(dividerItemDecoration)
        mAdapter.data = mData
        val emptyView = View.inflate(this, R.layout.empty_playing, null)
        emptyView.findViewById<TextView>(R.id.tv_desc).text = "暂无攻略与公告"
        mAdapter.setEmptyView(emptyView)
        mAdapter.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(this, GameActDetailActivity::class.java)
            intent.putExtra("id", mData[position].id)
            startActivity(intent)
        }
        binding.rcvActivity.adapter = mAdapter
    }

    //获取游戏公告
    private fun obtainGameActivity() {
        RetrofitManager.instance.obtainGameActivity("$mId", object :
            CustomResponseBodyObserver<ResponseBody>() {
            override fun onSubscribe(d: Disposable) {
                addDisposables(this@GameActActivity, d)
            }

            override fun specificHandle(responseBody: String) {
                val bean = GsonUtils.fromJson(responseBody, GameActivityResponse::class.java)

                if (bean.data.size > 0) {
                    mData.addAll(bean.data)
                }

                mAdapter.notifyDataSetChanged()
            }

            override fun handleException(ret: String, msg: String?) {
                msg?.let {
                    ToastUtils.showShort(this@GameActActivity, it)
                }
            }
        })
    }

    override fun initData() {
        obtainGameActivity()
    }

    override fun setListener() {
        binding.titleBar.ivBack.clickWithTrigger {
            finish()
        }
    }

    override fun onDestroy() {
        removeDisposables(this)
        super.onDestroy()
    }
}